package leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Liangyaoyun
 */
public class leetCode3 {

    public int lengthOfLongestSubstring(String s) {
        Set<Character>set=new HashSet<>();
        int len=s.length();
        int rightIndex=-1;
        int res=0;
        for (int i = 0; i < len; i++) {
            if (i!=0){
                //将下标为i-1的字符从set中删除
                set.remove(s.charAt(i-1));
            }
            //如果Set中未包含下标为rightIndex+1的重复元素则右指针往右边移动，并且将该字符加入Set中
            while (rightIndex+1<len&&!set.contains(s.charAt(rightIndex+1))){
                //将字符串s中下标为rightIndex+1的字符加入到set中
                set.add(s.charAt(rightIndex+1));
                rightIndex++;
            }
            //右指针下标减去当前下标起点+1是无重复字符的长度
            res=Math.max(res,rightIndex-i+1);
        }
        return res;
    }


    public static void main(String[] args) {
        String s = "abcabcbb";
        leetCode3 leetCode3 = new leetCode3();
        leetCode3.lengthOfLongestSubstring(s);
    }
}
